//
//  MapViewController.m
//  DuckAndDoc
//
//  Created by 蓝水软件 on 2017/5/9.
//  Copyright © 2017年 苏州蓝水有限公司. All rights reserved.
//

#import "MapViewController.h"
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>
#import "BaiduMapViewController.h"

@interface MapViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager *locationManager;
@property (strong, nonatomic) IBOutlet MKMapView *appleMapView;
@property (strong, nonatomic) IBOutlet UIButton *open;
@property (nonatomic,strong) CLLocation *location;  //当前定位信息
@property (nonatomic,strong) CLHeading *newsHeading;    //新磁场方向
@property (nonatomic,strong) UILabel *locationInfo;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}

- (void)setUI{
    [self.view addSubview:self.locationInfo];
    [self.locationInfo mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(self.open);
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(KSWidth);
    }];
    
    @weakify(self);
    [RACObserve(self, newsHeading) subscribeNext:^(CLHeading *newsHeading) {
        @strongify(self);
        self.locationInfo.text = [NSString stringWithFormat:@"%f",newsHeading.trueHeading];
    }];
    [self.open addTarget:self action:@selector(openLocation) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- buttonAction
- (void)openLocation{
    if (![CLLocationManager locationServicesEnabled]) {
        [JCAlertView showOneButtonWithTitle:@"提示" Message:@"当前设备不支持定位" ButtonType:JCAlertViewButtonTypeDefault ButtonTitle:@"知道了" Click:nil];
        return;
    }
    if ([UIDevice systemVersion] > 8.0) {
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    [self.locationManager startUpdatingLocation];
    [self.locationManager startUpdatingHeading];
}
- (IBAction)openBaiduMap:(id)sender {
    BaiduMapViewController *baiduMapController = [BaiduMapViewController new];
    [self.navigationController pushViewController:baiduMapController animated:YES];
}

#pragma mark -- CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    self.location = [locations lastObject];
    NSLog(@"定位成功：%@",self.location);
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    NSLog(@"定位失败");
}

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
    self.newsHeading = newHeading;
    NSLog(@"磁场方向：%@",newHeading);
}

#pragma mark -- 懒加载
- (CLLocationManager *)locationManager{
    if (_locationManager == nil) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
    }
    return _locationManager;
}

- (UILabel *)locationInfo{
    if (_locationInfo == nil) {
        _locationInfo = [UILabel new];
        _locationInfo.textAlignment = NSTextAlignmentCenter;
        _locationInfo.textColor = [UIColor flatGrayColor];
    }
    return _locationInfo;
}


@end
